// pages/mine/addAddress/addAddress.js
const app = getApp()
var util = require('../../../utils/util')

Page({

  /**
   * 页面的初始数据
   */


  data: {
    checked: false,
    // color1: "#ffffff",
    // color2: "#ffffff",
    cityname: "",
    list: [],
    id: "",
    shengname: "",
    dizhi: "填写上门地址",
    areaList: {
      province_list: {
        // //省
      },
      city_list: {
        // // 市
      },
      county_list: {
        // // 区
      }
    },
    // areaList:[],
    addressId: "",
    value: "",
    form: {
      name: "",
      phone: '',
      defalut: 0,
      address: '',
      area: '',
      sex: '',
      sign: ''
    },
    show: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    var addressId = options.addressId
    console.log(addressId)
    if (addressId) {
      that.setData({
        addressId: options.addressId
      })
      var postObj = {
        url: '/mobileUser/address/' + addressId,
        method: 'get'
      }
      app.request.wxRequest(postObj).then(res => {
        if (res.data.code === 200) {
          let data = res.data.data.sysAddress
          that.setData({
            id: res.data.data.areaId,
            form: data,
            value: data.area
          })
          console.log(that.data.id)
          that.setData({
            'form.area': that.data.id
          })
          if (data.defalut == 1)
            that.setData({
              checked: true
            })
          if (data.sex == 1) {
            that.setData({
              color1: "#808080",
              color2: "",
            })
          } else if (data.sex == 2) {
            that.setData({
              color1: "",
              color2: "#808080",
            })
          }
          if (data.sign == 1) {
            that.setData({
              color3: "#808080",
              color4: "",
              color5: "",
            })
          } else if (data.sign == 2) {
            that.setData({
              color3: "",
              color4: "#808080",
              color5: "",
            })

          } else if (data.sign == 3) {
            that.setData({
              color3: "",
              color4: "",
              color5: "#808080",
            })
          }
        }
      })

    }

    var postObj = {
      url: '/system/city/list',
      method: 'get'
    }
    app.request.wxRequest(postObj).then(res => {
      if (res.data.code === 200) {
        var list = res.data.data
        console.log(list)
        that.setData({
          list: list
        })
        // var city = JSON.parse(wx.getStorageSync('chooseCity'))
        var options = list
        var objList = {}
        options.forEach((items, i) => {
          var obj = app.transformObject(items.id, items.name)
          objList = {
            ...objList,
            ...obj
          }
          this.setData({
            // cityname: list[i].children[j].name,
            'areaList.province_list': objList
          })
        })
        //  for(var i = 0;i <list.length;i++)
        //     if(city.parentCityId == list[i].id)
        //     {   
        //       var obj = app.transformObject(city.parentCityId, list[i].name)
        //       this.setData({
        //       'areaList.province_list': obj ,
        //        shengname:list[i].name
        //       })
        //       for (var j = 0; j < list[i].children.length; j++)
        //         if (city.cityId == list[i].children[j].id)
        //         {  
        //           var obj = app.transformObject(city.cityId,list[i].children[j].name)
        //           this.setData({
        //           'areaList.city_list': obj ,
        //           })
        //           var options3 = list[i].children[j].children
        //           var objList3 = {}
        //           options3.forEach((items, i) => {
        //             var obj = app.transformObject(items.id, items.name)
        //             objList3 = {
        //               ...objList3,
        //               ...obj
        //             }
        //           })
        //           console.log(objList3)
        //           this.setData({
        //             cityname: list[i].children[j].name,
        //            'areaList.county_list': objList3

        //           })
        //           console.log(list[i].children[j].children)
        //         }

        //     }



        // console.log(this.data.addressList)
      }

    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onChange({
    detail
  }) {
    // 需要手动对 checked 状态进行更新
    this.setData({
      checked: detail
    });

  },

  //监听值变化
  onChange1(e) {
    var c = e.detail
    this.setData({
      'form.name': c
    })

  },

  onChange2(e) {
    var c = e.detail
    this.setData({
      'form.phone': c
    })
  },

  onChange3(e) {
    var c = e.detail
    this.setData({
      'form.area': c
    })
  },

  onChange4(e) {
    var c = e.detail
    this.setData({
      'form.address': c
    })
  },
  //性别
  nan() {
    this.setData({
      'form.sex': 1,
      color1: "#808080",
      color2: "",
    })
  },
  nv() {
    this.setData({
      'form.sex': 2,
      color1: "",
      color2: "#808080",

    })
  },
  //标签
  gongsi() {
    this.setData({
      'form.sign': 1,
      color3: "#808080",
      color4: "",
      color5: "",
    })
  },
  home() {
    this.setData({
      'form.sign': 2,
      color3: "",
      color4: "#808080",
      color5: "",
    })
  },
  school() {
    this.setData({
      'form.sign': 3,
      color3: "",
      color4: "",
      color5: "#808080",
    })
  },
  //保存
  baocun() {
    if (!this.data.form.name) {
      wx.showToast({
        title: '请填写联系人',
        image: '../../../assets/error.png',
      })
    } else
    if (!this.data.form.phone) {
      wx.showToast({
        title: '请填写电话',
        image: '../../../assets/error.png',
      })
    } else 
    if (!this.data.form.area) {
      wx.showToast({
        title: '请选择地址',
        image: '../../../assets/error.png',
      })
    } else if (!this.data.form.address) {
      wx.showToast({
        title: '请填写门牌号',
        image: '../../../assets/error.png',
      })
    } else {
     if(this.data.checked == true){
       this.setData({
         'form.defalut':1,
       })
     }
     if(this.data.addressId){
      var postObj = {
        url: '/mobileUser/address',
        method: 'put',
        data: this.data.form
      }
      app.request.wxRequest(postObj).then(res => {
        if (res.data.code === 200) {
          wx.navigateBack({
            delta: -1
          })
        }
      })
    
     }
     else {
      var postObj = {
        url: '/mobileUser/address',
        method: 'post',
        data: this.data.form
      }
      app.request.wxRequest(postObj).then(res => {
        if (res.data.code === 200) 
          wx.navigateBack({
            delta: -1
          })
    
   
      })
  }
  }
},
tocity() {
  this.setData({
    show: true
  })
},
onClose() {
  this.setData({
    show: false
  });
},


onConfirm(event) {
  // const { picker, value, index } = event.detail;
  // Toast(`当前值：${value}, 当前索引：${index}`);
  this.setData({
    show: false
  });
},

onCancel() {
  this.setData({
    show: false
  });

},
change10(e) {
  var list = this.data.list
  var a = e.detail.values[0]
  var b = e.detail.values[1]
  var c = e.detail.values[2]
  if (a) {
    for (var i = 0; i < list.length; i++)
      if (a.code == list[i].id) {
        var options1 = list[i].children
        var objList1 = {}
        options1.forEach((items, i) => {
          var obj1 = app.transformObject(items.id, items.name)
          objList1 = {
            ...objList1,
            ...obj1
          }
          this.setData({
            // cityname: list[i].children[j].name,
            'areaList.city_list': objList1,
            //  'areaList.county_list':[]
          })

        })
        if (b) {
          for (var j = 0; j < list[i].children.length; j++)
            if (b.code == list[i].children[j].id) {
              var options2 = list[i].children[j].children
              var objList2 = {}
              options2.forEach((items, i) => {
                var obj2 = app.transformObject(items.id, items.name)
                objList2 = {
                  ...objList2,
                  ...obj2
                }
                this.setData({
                  // cityname: list[i].children[j].name,
                  //  'areaList.city_list': objList1,
                  'areaList.county_list': objList2
                })

              })
            }
          if (c) {
            this.setData({
              'form.area': c.code,
              value: a.name + b.name + c.name
            })
          }
        }

      }
  }

  // this.setData({
  //   'form.area':c.code,
  //   value:this.data.shengname +this.data.cityname +css.name
  // })

},
shanchu(e) {
  var that = this
  wx.showModal({
    title: '警告',
    content: '确认删除吗',
    success(res) {
      if (res.confirm) {
        var shanchuList = {
          url: '/mobileUser/address/' + that.data.addressId,
          method: 'delete',
        }
        app.request.wxRequest(shanchuList).then(res => {

          if (res.data.code === 200) {
            wx.navigateBack({
              delta: -1
            })

          }
        });

      } else if (res.cancel) {
        console.log('用户点击取消')
      }

    }
  })
},
})